package likou;

public class _647 {
    public static void main(String[] args) {
        System.out.println(countSubstrings("aaa"));
        System.out.println(countSubstrings2("aaa"));
    }
    //求回文子串的个数，子串是相连的，其实可以用波及来做的，下面的做法是暴力，遍历所有情况
    public static int countSubstrings(String s) {
        int res = 0;
        for(int i=2;i<=s.length();i++){
            for (int j=0;j<s.length()-i+1;j++){
                String substring = s.substring(j, i + j);
                if(new StringBuilder(substring).reverse().toString().equals(substring)) res++;
            }
        }
        res+=s.length();
        return res;
    }

    public static int countSubstrings2(String s) {
        int res = s.length();
        char[] cs = s.toCharArray();
        for(int i=0;i<cs.length;i++){
            int left = i-1,right=i+1;
            while(left>=0&&right<cs.length&&cs[right]==cs[left]){
                res++;left--;right++;
            }
            left = i;right = i+1;
            while(left>=0&&right<cs.length&&cs[right]==cs[left]){
                res++;left--;right++;
            }
        }
        return res;
    }

}
